package com.fourmob.poppyview.sample.slice;

import com.fourmob.poppyview.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import com.fourmob.poppyview.librarys.PoppyViewHelper;
import ohos.agp.components.*;
import ohos.agp.window.dialog.ToastDialog;

public class ListViewAbilitySlice extends AbilitySlice {
    private static final String[] songs = { "1983... (A Merman I Should Turn to Be)", "51st Anniversary", "All Along The Watchtower", "Angel", "Are You Experienced?", "Bleeding Heart", "Bold As Love", "Burning Of The Midnight Lamp",
            "Castles Made Of Sand", "Crash Landing", "Crosstown Traffic", "Dolly Dagger", "Drifting", "Fire", "Foxy Lady", "Gypsy Eyes", "Hear My Train a Comin'", "Hey Joe", "Highway Chile", "House Burning Down", "Izabella", "Let Me Move You",
            "Little Wing", "Lover Man", "Machine Gun", "Manic Depression", "Mojo Man", "Mr. Bad Luck", "One Rainy Wish", "Purple Haze", "Red House", "She's So Fine", "Ships Passing In The Night", "Somewhere", "Spanish Castle Magic",
            "Stepping Stone", "Still Raining, Still Dreaming", "Stone Free", "The Wind Cries Mary", "Third Stone From The Sun", "Valleys of Neptune", "Voodoo Child (Slight Return)" };

    private PoppyViewHelper mPoppyViewHelper;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_listview);
        mPoppyViewHelper = new PoppyViewHelper(getAbility());
        Component poppyView = mPoppyViewHelper.createPoppyViewOnListView(ResourceTable.Id_listView1, ResourceTable.Layout_poppyview, new Component.ScrolledListener()  {
            @Override
            public void onContentScrolled(Component component, int i, int i1, int i2, int i3) {

            }
//
            @Override
            public void scrolledStageUpdate(Component component, int newStage) {

            }

        });
        poppyView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ToastDialog toastDialog =new ToastDialog(getContext());
                toastDialog.setText("Click me!");
                toastDialog.show();
            }
        });

        final ListContainer listView1 = (ListContainer)findComponentById(ResourceTable.Id_listView1);
        listView1.setItemProvider(new BaseItemProvider() {
            @Override
            public int getCount() {
                return songs.length;
            }
            @Override
            public Object getItem(int i) {
                return songs[i];
            }
            @Override
            public long getItemId(int i) {
                return songs[i].hashCode();
            }
            @Override
            public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
                if(component == null) {
                    component=LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_listitem_song, componentContainer,false);
                }
                ((Text) component).setText((String) getItem(i));
                return component;
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
